﻿Imports System.Text
Imports CQRSAzure.EventSourcing
Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports CQRSAzure.EventSourcing.InMemory

<TestClass()>
Public Class InMemoryEventStreamUnitTest

    <TestMethod()>
    Public Sub Reader_Constructor_TestMethod()

        Dim testObj As InMemoryEventStreamReader(Of MockAggregate, String) = InMemoryEventStreamReader(Of MockAggregate, String).Create(New MockAggregate("123"))
        Assert.IsNotNull(testObj)

    End Sub


    <TestMethod()>
    Public Sub Writer_Constructor_TestMethod()

        Dim testObj As InMemoryEventStreamWriter(Of MockAggregate, String) = InMemoryEventStreamWriter(Of MockAggregate, String).Create(New MockAggregate("123"))
        Assert.IsNotNull(testObj)

    End Sub

    <TestMethod()>
    Public Sub Writer_Add_RecordCount_TestMethod()

        Dim expected As Integer = 1
        Dim actual As Integer = 0

        Dim testObj As InMemoryEventStreamWriter(Of MockAggregate, String) = InMemoryEventStreamWriter(Of MockAggregate, String).Create(New MockAggregate("123"))
        'add an event
        testObj.AppendEvent(New MockEventTypeOne())
        actual = testObj.RecordCount

        Assert.AreEqual(expected, actual)

    End Sub

    <TestMethod()>
    Public Sub Writer_Add_Reader_Read_TestMethod()

        Dim expected As Integer = 1
        Dim actual As Integer = 0

        Dim testEvt As New MockEventTypeOne()
        testEvt.EventOneIntegerProperty = expected

        Dim testWriteObj As InMemoryEventStreamWriter(Of MockAggregate, String) = InMemoryEventStreamWriter(Of MockAggregate, String).Create(New MockAggregate("912"))
        'add an event
        testWriteObj.AppendEvent(testEvt)

        Dim testReadObj As InMemoryEventStreamReader(Of MockAggregate, String) = InMemoryEventStreamReader(Of MockAggregate, String).Create(New MockAggregate("912"))
        Dim readObj As MockEventTypeOne = testReadObj.GetEvents().LastOrDefault()

        actual = readObj.EventOneIntegerProperty

        Assert.AreEqual(expected, actual)

    End Sub


    <TestMethod()>
    Public Sub Writer_AddMultiple_Reader_Read_TestMethod()

        Dim expected As Integer = 1
        Dim actual As Integer = 0

        Dim testEvt As New MockEventTypeOne()
        testEvt.EventOneIntegerProperty = expected

        Dim testWriteObj As InMemoryEventStreamWriter(Of MockAggregate, String) = InMemoryEventStreamWriter(Of MockAggregate, String).Create(New MockAggregate("9992"))

        testWriteObj.AppendEvent(New MockEventTypeOne())
        testWriteObj.AppendEvent(New MockEventTypeTwo())
        testWriteObj.AppendEvent(New MockEventTypeOne())
        testWriteObj.AppendEvent(New MockEventTypeTwo())

        'add our test event
        testWriteObj.AppendEvent(testEvt)

        Dim testReadObj As InMemoryEventStreamReader(Of MockAggregate, String) = InMemoryEventStreamReader(Of MockAggregate, String).Create(New MockAggregate("9992"))
        Dim readObj As MockEventTypeOne = testReadObj.GetEvents().LastOrDefault()

        actual = readObj.EventOneIntegerProperty

        Assert.AreEqual(expected, actual)

    End Sub

    <TestMethod()>
    Public Sub Writer_AddMultiple_Reader_ReadWrapped_TestMethod()

        Dim expected As Integer = 1
        Dim actual As Integer = 0

        Dim testEvt As New MockEventTypeOne()
        testEvt.EventOneIntegerProperty = expected

        Dim testWriteObj As InMemoryEventStreamWriter(Of MockAggregate, String) = InMemoryEventStreamWriter(Of MockAggregate, String).Create(New MockAggregate("9992"))

        testWriteObj.AppendEvent(New MockEventTypeOne())
        testWriteObj.AppendEvent(New MockEventTypeTwo())
        testWriteObj.AppendEvent(New MockEventTypeOne())
        testWriteObj.AppendEvent(New MockEventTypeTwo())

        'add our test event
        testWriteObj.AppendEvent(testEvt)

        Dim testReadObj As InMemoryEventStreamReader(Of MockAggregate, String) = InMemoryEventStreamReader(Of MockAggregate, String).Create(New MockAggregate("9992"))
        Dim readObj = testReadObj.GetEventsWithContext().LastOrDefault()

        actual = CTypeDynamic(Of MockEventTypeOne)(readObj.EventInstance).EventOneIntegerProperty

        Assert.AreEqual(expected, actual)

    End Sub

End Class